const label = '运动员信息'
const name = 'sportsman'
let i = 1
const uniqueId = () => `${name}${i++}`
import { makeAddControl } from '../addControl'
const box = {
  label,
  //拖拽组件的 key
  name,
  //拖拽组件的生成规则
  rule() {
    //如果在 props 方法中需要修改 rule 的属性,需要提前在 rule 上定义对应的属性
    return {
      //生成组件的名称
      type: 'span',
      //field 自定不能重复,所以这里每次都会生成一个新的
      field: uniqueId(),
      title: '运动员',
      effect: {
        req: false
      },
      props: {
        placeholder: '请输入'
      },
      control: [
        {
          handle: () => true,
          rule: [
            {
              type: 'input',
              field: uniqueId(),
              title: '姓名',
              props: {
                placeholder: '请输入'
              },
              update(val, rule, fApi) {
                const parent = fApi.getRule(
                  rule.field.replace(/\d+/, val => Number(val) - 1)
                )
                if (parent.effect.req) {
                  rule.$required = '请输入姓名'
                } else {
                  rule.$required = false
                }
              }
            }
          ]
        },
        {
          handle: () => true,
          rule: [
            {
              type: 'radio',
              field: uniqueId(),
              title: '性别',
              options: [
                { value: '男', label: '男' },
                { value: '女', label: '女' }
              ],
              update(val, rule, fApi) {
                const parent = fApi.getRule(
                  rule.field.replace(/\d+/, val => Number(val) - 2)
                )
                if (parent.effect.req) {
                  rule.$required = '请选择性别'
                } else {
                  rule.$required = false
                }
              }
            }
          ]
        }
      ]
    }
  },
  //拖拽组件配置项(props)的生成规则
  props(rule) {
    return [
      {
        type: 'switch',
        field: 'formCreateEffect>req',
        value: false,
        title: '是否必填',
        options: [{ value: false }, { value: true }]
      },
      makeAddControl(rule, uniqueId)
    ]
  }
}
export default box
